package org.andios.jdbc;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBC {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        driver = ResourceBundle.getBundle("db").getString("driver");
        url = ResourceBundle.getBundle("db").getString("url");
        username = ResourceBundle.getBundle("db").getString("username");
        password = ResourceBundle.getBundle("db").getString("password");
    }

    public static void main(String[] args) {
        Connection connection = null;//设置连接信息
        PreparedStatement preparedStatement = null;//执行sql语句
        ResultSet resultSet = null;//返回的结果集
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "SELECT * FROM user";//user是test数据库下的表名。
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(
                        resultSet.getString("id") + " " +
                                resultSet.getString("username") + " " +
                                resultSet.getString("age") + " "
                );
            }
        } catch (Exception e) {
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
